<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- clocks.qdoc -->
  <title>Qt Quick Demo - Clocks | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Qt Quick Demo - Clocks</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#displaying-data-generated-by-list-models">Displaying Data Generated by List Models</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Demo - Clocks</h1>
<span class="subtitle"></span>
<!-- $$$demos/clocks-brief -->
<p>A QML clock application that demonstrates using a <a href="../qtquick/qml-qtquick-listview.html">ListView</a> type to display data generated by a <a href="../qtqmlmodels/qml-qtqml-models-listmodel.html">ListModel</a> and a <a href="../qtquick/qml-qtquick-springanimation.html">SpringAnimation</a> type to animate images.</p>
<!-- @@@demos/clocks -->
<!-- $$$demos/clocks-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qtquick-demo-clocks-small.png" alt="" /></p><p><i>Clocks</i> demonstrates using a <a href="../qtquick/qml-qtquick-listview.html">ListView</a> type to display data generated by a <a href="../qtqmlmodels/qml-qtqml-models-listmodel.html">ListModel</a>. The delegate used by the model is specified as a custom QML type that is specified in the Clock.qml file.</p>
<p>JavaScript methods are used to fetch the current time in several cities in different time zones and QML types are used to display the time on a clock face with animated clock hands.</p>
<a name="running-the-example"></a>
<h4 id="running-the-example">Running the Example</h4>
<p>To run the example from <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<a name="displaying-data-generated-by-list-models"></a>
<h4 id="displaying-data-generated-by-list-models">Displaying Data Generated by List Models</h4>
<p>In the clocks.qml file, we use a <a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a> type to create the application main window:</p>
<pre class="cpp">

  <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">root</span>
      <span class="name">width</span>: <span class="number">640</span>; <span class="name">height</span>: <span class="number">320</span>
      <span class="name">color</span>: <span class="string">&quot;#646464&quot;</span>

</pre>
<p>We use a <a href="../qtquick/qml-qtquick-listview.html">ListView</a> type to display a list of the items provided by a <a href="../qtqmlmodels/qml-qtqml-models-listmodel.html">ListModel</a> type:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-listview.html">ListView</a></span> {
          <span class="name">id</span>: <span class="name">clockview</span>
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">orientation</span>: <span class="name">ListView</span>.<span class="name">Horizontal</span>
          <span class="name">cacheBuffer</span>: <span class="number">2000</span>
          <span class="name">snapMode</span>: <span class="name">ListView</span>.<span class="name">SnapOneItem</span>
          <span class="name">highlightRangeMode</span>: <span class="name">ListView</span>.<span class="name">ApplyRange</span>

          <span class="name">delegate</span>: <span class="name">Content</span>.Clock { <span class="name">city</span>: <span class="name">cityName</span>; <span class="name">shift</span>: <span class="name">timeShift</span> }
          <span class="name">model</span>: <span class="name">ListModel</span> {
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;New York&quot;</span>; <span class="name">timeShift</span>: -<span class="number">4</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;London&quot;</span>; <span class="name">timeShift</span>: <span class="number">0</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;Oslo&quot;</span>; <span class="name">timeShift</span>: <span class="number">1</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;Mumbai&quot;</span>; <span class="name">timeShift</span>: <span class="number">5.5</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;Tokyo&quot;</span>; <span class="name">timeShift</span>: <span class="number">9</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;Brisbane&quot;</span>; <span class="name">timeShift</span>: <span class="number">10</span> }
              <span class="type"><a href="../qtqmlmodels/qml-qtqml-models-listelement.html">ListElement</a></span> { <span class="name">cityName</span>: <span class="string">&quot;Los Angeles&quot;</span>; <span class="name">timeShift</span>: -<span class="number">8</span> }
          }
      }

</pre>
<p>List elements are defined like other QML types except that they contain a collection of <i>role</i> definitions instead of properties. Roles both define how the data is accessed and include the data itself.</p>
<p>For each list element, we use the <code>cityName</code> role to specify the name of a city and the <code>timeShift</code> role to specify a time zone as a positive or negative offset from UTC (coordinated universal time).</p>
<p>The Clock custom type is used as the <a href="../qtquick/qml-qtquick-listview.html">ListView</a>'s <code>delegate</code>, defining the visual appearance of list items. To use the Clock type, we add an import statement that imports the folder called <code>content</code> where the type is located:</p>
<pre class="cpp">

  import &quot;content&quot; as Content

</pre>
<p>We use an <a href="../qdoc/09-qdoc-commands-includingimages.html#image">Image</a> type to display arrows that indicate whether users can flick the view to see more clocks on the left or right:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
          <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span>
          <span class="name">anchors</span>.bottom: <span class="name">parent</span>.<span class="name">bottom</span>
          <span class="name">anchors</span>.margins: <span class="number">10</span>
          <span class="name">source</span>: <span class="string">&quot;content/arrow.png&quot;</span>
          <span class="name">rotation</span>: -<span class="number">90</span>
          <span class="name">opacity</span>: <span class="name">clockview</span>.<span class="name">atXBeginning</span> ? <span class="number">0</span> : <span class="number">0.5</span>
          Behavior on <span class="name">opacity</span> { <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">500</span> } }
      }

      <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
          <span class="name">anchors</span>.right: <span class="name">parent</span>.<span class="name">right</span>
          <span class="name">anchors</span>.bottom: <span class="name">parent</span>.<span class="name">bottom</span>
          <span class="name">anchors</span>.margins: <span class="number">10</span>
          <span class="name">source</span>: <span class="string">&quot;content/arrow.png&quot;</span>
          <span class="name">rotation</span>: <span class="number">90</span>
          <span class="name">opacity</span>: <span class="name">clockview</span>.<span class="name">atXEnd</span> ? <span class="number">0</span> : <span class="number">0.5</span>
          Behavior on <span class="name">opacity</span> { <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">500</span> } }
      }
  }

</pre>
<p>We use the <code>opacity</code> property to hide the arrows when the list view is located at the beginning or end of the x axis.</p>
<p>In Clock.qml, we define a <code>timeChanged()</code> function in which we use methods from the JavaScript <code>Date</code> object to fetch the current time in UTC and to adjust it to the correct time zone:</p>
<pre class="cpp">

      <span class="keyword">function</span> <span class="name">timeChanged</span>() {
          var date = new <span class="name">Date</span>;
          <span class="name">hours</span> <span class="operator">=</span> <span class="name">internationalTime</span> ? <span class="name">date</span>.<span class="name">getUTCHours</span>() <span class="operator">+</span> <span class="name">Math</span>.<span class="name">floor</span>(<span class="name">clock</span>.<span class="name">shift</span>) : <span class="name">date</span>.<span class="name">getHours</span>()
          <span class="name">night</span> <span class="operator">=</span> ( <span class="name">hours</span> <span class="operator">&lt;</span> <span class="number">7</span> <span class="operator">||</span> <span class="name">hours</span> <span class="operator">&gt;</span> <span class="number">19</span> )
          <span class="name">minutes</span> <span class="operator">=</span> <span class="name">internationalTime</span> ? <span class="name">date</span>.<span class="name">getUTCMinutes</span>() <span class="operator">+</span> ((<span class="name">clock</span>.<span class="name">shift</span> <span class="operator">%</span> <span class="number">1</span>) <span class="operator">*</span> <span class="number">60</span>) : <span class="name">date</span>.<span class="name">getMinutes</span>()
          <span class="name">seconds</span> <span class="operator">=</span> <span class="name">date</span>.<span class="name">getUTCSeconds</span>();
      }

</pre>
<p>We use a <a href="../qtqml/qml-qtqml-timer.html">Timer</a> type to update the time at intervals of 100 milliseconds:</p>
<pre class="cpp">

      <span class="type"><a href="../qtqml/qml-qtqml-timer.html">Timer</a></span> {
          <span class="name">interval</span>: <span class="number">100</span>; <span class="name">running</span>: <span class="number">true</span>; <span class="name">repeat</span>: <span class="number">true</span>;
          <span class="name">onTriggered</span>: <span class="name">clock</span>.<span class="name">timeChanged</span>()
      }

</pre>
<p>We use <a href="../qdoc/09-qdoc-commands-includingimages.html#image">Image</a> types within an <a href="../qtquick/qml-qtquick-item.html">Item</a> type to display the time on an analog clock face. Different images are used for daytime and nighttime hours:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
          <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
          <span class="name">width</span>: <span class="number">200</span>; <span class="name">height</span>: <span class="number">240</span>

          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">background</span>; <span class="name">source</span>: <span class="string">&quot;clock.png&quot;</span>; <span class="name">visible</span>: <span class="name">clock</span>.<span class="name">night</span> <span class="operator">==</span> <span class="number">false</span> }
          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> { <span class="name">source</span>: <span class="string">&quot;clock-night.png&quot;</span>; <span class="name">visible</span>: <span class="name">clock</span>.<span class="name">night</span> <span class="operator">==</span> <span class="number">true</span> }

</pre>
<p>A <a href="../qtquick/qml-qtquick-rotation.html">Rotation</a> transform applied to <a href="../qdoc/09-qdoc-commands-includingimages.html#image">Image</a> types provides a way to rotate the clock hands. The <code>origin</code> property holds the point that stays fixed relative to the parent as the rest of the item rotates. The <code>angle</code> property determines the angle to rotate the hands in degrees clockwise.</p>
<pre class="cpp">

          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
              <span class="name">x</span>: <span class="number">92.5</span>; <span class="name">y</span>: <span class="number">27</span>
              <span class="name">source</span>: <span class="string">&quot;hour.png&quot;</span>
              <span class="name">transform</span>: <span class="name">Rotation</span> {
                  <span class="name">id</span>: <span class="name">hourRotation</span>
                  <span class="name">origin</span>.x: <span class="number">7.5</span>; <span class="name">origin</span>.y: <span class="number">73</span>;
                  <span class="name">angle</span>: (<span class="name">clock</span>.<span class="name">hours</span> <span class="operator">*</span> <span class="number">30</span>) <span class="operator">+</span> (<span class="name">clock</span>.<span class="name">minutes</span> <span class="operator">*</span> <span class="number">0.5</span>)
                  Behavior on <span class="name">angle</span> {
                      <span class="type"><a href="../qtquick/qml-qtquick-springanimation.html">SpringAnimation</a></span> { <span class="name">spring</span>: <span class="number">2</span>; <span class="name">damping</span>: <span class="number">0.2</span>; <span class="name">modulus</span>: <span class="number">360</span> }
                  }
              }
          }

          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
              <span class="name">x</span>: <span class="number">93.5</span>; <span class="name">y</span>: <span class="number">17</span>
              <span class="name">source</span>: <span class="string">&quot;minute.png&quot;</span>
              <span class="name">transform</span>: <span class="name">Rotation</span> {
                  <span class="name">id</span>: <span class="name">minuteRotation</span>
                  <span class="name">origin</span>.x: <span class="number">6.5</span>; <span class="name">origin</span>.y: <span class="number">83</span>;
                  <span class="name">angle</span>: <span class="name">clock</span>.<span class="name">minutes</span> <span class="operator">*</span> <span class="number">6</span>
                  Behavior on <span class="name">angle</span> {
                      <span class="type"><a href="../qtquick/qml-qtquick-springanimation.html">SpringAnimation</a></span> { <span class="name">spring</span>: <span class="number">2</span>; <span class="name">damping</span>: <span class="number">0.2</span>; <span class="name">modulus</span>: <span class="number">360</span> }
                  }
              }
          }

          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
              <span class="name">x</span>: <span class="number">97.5</span>; <span class="name">y</span>: <span class="number">20</span>
              <span class="name">source</span>: <span class="string">&quot;second.png&quot;</span>
              <span class="name">transform</span>: <span class="name">Rotation</span> {
                  <span class="name">id</span>: <span class="name">secondRotation</span>
                  <span class="name">origin</span>.x: <span class="number">2.5</span>; <span class="name">origin</span>.y: <span class="number">80</span>;
                  <span class="name">angle</span>: <span class="name">clock</span>.<span class="name">seconds</span> <span class="operator">*</span> <span class="number">6</span>
                  Behavior on <span class="name">angle</span> {
                      <span class="type"><a href="../qtquick/qml-qtquick-springanimation.html">SpringAnimation</a></span> { <span class="name">spring</span>: <span class="number">2</span>; <span class="name">damping</span>: <span class="number">0.2</span>; <span class="name">modulus</span>: <span class="number">360</span> }
                  }
              }
          }

          <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
              <span class="name">anchors</span>.centerIn: <span class="name">background</span>; <span class="name">source</span>: <span class="string">&quot;center.png&quot;</span>
          }

</pre>
<p>We use a <a href="../qtquick/qml-qtquick-behavior.html">Behavior</a> type on the <code>angle</code> property to apply a <a href="../qtquick/qml-qtquick-springanimation.html">SpringAnimation</a> when the time changes. The <code>spring</code> and <code>damping</code> properties enable the spring-like motion of the clock hands, and a <code>modulus</code> of <code>360</code> makes the animation target values wrap around at a full circle.</p>
<p>We use a <a href="../qtquick/qml-qtquick-text.html">Text</a> type to display the city name below the clock:</p>
<pre class="cpp">

          <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> {
              <span class="name">id</span>: <span class="name">cityLabel</span>
              <span class="name">y</span>: <span class="number">210</span>; <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span>
              <span class="name">color</span>: <span class="string">&quot;white&quot;</span>
              <span class="name">font</span>.family: <span class="string">&quot;Helvetica&quot;</span>
              <span class="name">font</span>.bold: <span class="number">true</span>; <span class="name">font</span>.pixelSize: <span class="number">16</span>
              <span class="name">style</span>: <span class="name">Text</span>.<span class="name">Raised</span>; <span class="name">styleColor</span>: <span class="string">&quot;black&quot;</span>
          }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtdoc-demos-clocks-clocks-pro.html">demos/clocks/clocks.pro</a></li>
<li><a href="qtdoc-demos-clocks-clocks-qml.html">demos/clocks/clocks.qml</a></li>
<li><a href="qtdoc-demos-clocks-clocks-qmlproject.html">demos/clocks/clocks.qmlproject</a></li>
<li><a href="qtdoc-demos-clocks-clocks-qrc.html">demos/clocks/clocks.qrc</a></li>
<li><a href="qtdoc-demos-clocks-content-clock-qml.html">demos/clocks/content/Clock.qml</a></li>
<li><a href="qtdoc-demos-clocks-main-cpp.html">demos/clocks/main.cpp</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/demos/clocks/content/arrow.png">demos/clocks/content/arrow.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/background.png">demos/clocks/content/background.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/center.png">demos/clocks/content/center.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/clock-night.png">demos/clocks/content/clock-night.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/clock.png">demos/clocks/content/clock.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/hour.png">demos/clocks/content/hour.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/minute.png">demos/clocks/content/minute.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/quit.png">demos/clocks/content/quit.png</a></li>
<li><a href="images/used-in-examples/demos/clocks/content/second.png">demos/clocks/content/second.png</a></li>
</ul>
</div>
<p><b>See also </b><a href="qmlapplications.html">QML Applications</a>.</p>
<!-- @@@demos/clocks -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
